package com.smart.controller;


import com.smart.entity.Employee;
import com.smart.entity.Result;
import com.smart.service.IEmployeeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Cookies
 * @since 2024-05-06
 */
@RestController
@CrossOrigin
@RequestMapping("/employee")
@Api(tags = "员工相关接口")
public class EmployeeController {
    @Autowired
    private IEmployeeService employeeService;

    @PostMapping("/{current}/{size}")
    @ApiOperation(value = "根据检索条件分页获取员工列表", response = Result.class)
    public Result getEmployeeList(
            @PathVariable @ApiParam(name = "当前页", required = true) Integer current,
            @PathVariable @ApiParam(name = "每页数量", required = true) Integer size,
            @RequestBody(required = false) @ApiParam(name = "员工检索条件") Employee condition) {
        if (current == null || size == null) {
            return Result.error("参数不能为空");
        }
        return Result.success(employeeService.getEmployeeList(condition, current, size));
    }

    @GetMapping("/count")
    @ApiOperation(value = "获取员工数量", response = Result.class)
    public Result getDeviceCount() {
        return Result.success(employeeService.count());
    }

    @GetMapping("/attendance")
    @ApiOperation(value = "获取员工考勤数据统计", response = Result.class)
    public Result getAttendanceData() {
        return Result.success(employeeService.getAttendanceData());
    }

    @PostMapping
    @ApiOperation(value = "添加员工", response = Result.class)
    public Result addEmployee(
            @RequestBody @ApiParam(name = "员工信息", required = true) Employee employee) {
        boolean isSucceed = employeeService.addEmployee(employee);
        return isSucceed ? Result.success() : Result.error("操作失败!");
    }

    @PutMapping
    @ApiOperation(value = "修改员工信息", response = Result.class)
    public Result updateEmployee(
            @RequestBody @ApiParam(name = "员工信息", required = true) Employee employee) {
        boolean isSucceed = employeeService.updateById(employee);
        return isSucceed ? Result.success() : Result.error("操作失败!");
    }

    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除单个员工", response = Result.class)
    public Result deleteEmployee(
            @PathVariable @ApiParam(name = "员工ID", required = true) Integer id) {
        boolean isSucceed = employeeService.removeById(id);
        return isSucceed ? Result.success() : Result.error("操作失败!");
    }

    @PostMapping("/deleteBatch")
    @ApiOperation(value = "批量删除员工", response = Result.class)
    public Result deleteEmployees(
            @RequestBody @ApiParam(name = "员工ID列表", required = true) List<Integer> ids) {
        boolean isSucceed = employeeService.removeByIds(ids);
        return isSucceed ? Result.success() : Result.error("操作失败!");
    }
}
